<?php
require 'BaseController.php';
class Admin_ProductController extends Admin_BaseController{
	private $_identity;
	private $_productModel;
	private $_categoryModel;
	private $_filterModel;
	private $_manModel;
	
	public function init(){
		parent::init();
		$auth = Zend_Auth::getInstance();
		$this->_identity = $auth->getIdentity();
		$this->view->identity = $this->_identity;
		$this->_categoryModel = new Model_PCategory();
		$this->_productModel = new Model_Product();
		$this->_manModel = new Model_Manufacturer();
		$this->_filterModel = new Model_Filter();
		
	}
	public function listAction(){
		$session = new Zend_Session_Namespace();
		$this->view->message = $session->message;
		$session->__unset('message');
		//lấy các category
		$this->view->arrCategories = $this->_categoryModel->getAllCategories();
		$this->view->arrManufacturers = $this->_manModel->getAllManufacturers();
		//lấy các tham số
		$man_id = $cat_id = 0;
		$model = "";
		if($this->getRequest()->isPost()){
			$man_id = $this->getRequest()->getParam("man_id");
			$cat_id = $this->getRequest()->getParam("cat_id");
			$model = $this->getRequest()->getParam("model");
		}
		//Đưa danh sách product vào Zend_Paginator để bắt đầu phân trang
        $paginator = Zend_Paginator::factory($this->_productModel->getAllProducts($man_id, $cat_id, $model));
        //Số product trên một trang
        $paginator->setItemCountPerPage(20);
        //Số trang được hiện ra để click
        $paginator->setPageRange(5);
        //Lấy trang hiện tại
        $currentPage = $this->getRequest()->getParam('page',1);
        $paginator->setCurrentPageNumber($currentPage);
        
        //Truyền dữ liệu ra view
        $this->view->arrProducts=$paginator;
	}
	public function createAction(){
		$this->view->headScript()->appendFile($this->view->baseUrl().'/public/plugins/ckeditor/ckeditor.js');
		$this->view->headScript()->appendFile($this->view->baseUrl().'/public/plugins/ckfinder/ckfinder.js');
		$this->view->headLink()->appendStylesheet($this->view->baseUrl().'/public/admin/css/aearticle.css');
		
		$session = new Zend_Session_Namespace();
		//lấy các category
		$this->view->arrCategories = $this->_categoryModel->getAllCategories();
		$this->view->arrManufacturers = $this->_manModel->getAllManufacturers();
		//kiểm tra có dữ liệu post lên không
		$request = $this->getRequest();
		if($this->getRequest()->isPost()){
			//lấy dữ liệu trên form
			$name= $request->getParam('name');
			$model= $request->getParam('model');
			$price= $request->getParam('price');
			$show_on_home = 0;
					if($request->getParam('show_on_home')){
						$show_on_home = 1;
					}
			$image = $request->getParam('image');
			$image = join('@', $image);
			$specification= $request->getParam('specification');
			$ink= $request->getParam('ink');
			$slug = $request->getParam('slug');
			$components= $request->getParam('components');
			$description= $request->getParam('description');
			$short_description= $request->getParam('short_description');
			$page_title= $request->getParam('page_title');
			$page_description= $request->getParam('page_description');
			$category_id = $request->getParam('category_id');
			$feature_image = $request->getParam('feature_image');
			$created_date = date('Y-m-d', time());
			$man_id = $request->getParam('man_id');
			$tag = $request->getParam('tag');
			$filter = $request->getParam('filter');
			
			$id = $this->_productModel->createProduct(
					$name, $model, $price,$show_on_home, $image, 
					$specification, $ink, $components,
					 $description, $short_description,
					 $page_title, $page_description, $created_date, $man_id, $slug, $category_id, $tag,$filter);
			if($id){
				$session->message = '<span class="success">Tạo sản phẩm mới thành công</span>';
				$this->_redirector->gotoRoute(array(), 'admin_product_list');
			}else{
				$this->view->message = '<span class="error">Tạo sản phẩm mới không thành công</span>';
			}
		}
	}
	public function editAction(){
		$session = new Zend_Session_Namespace(); 
		$this->view->headScript()->appendFile($this->view->baseUrl().'/public/plugins/ckeditor/ckeditor.js');
		$this->view->headScript()->appendFile($this->view->baseUrl().'/public/plugins/ckfinder/ckfinder.js');
		$this->view->headLink()->appendStylesheet($this->view->baseUrl().'/public/admin/css/aearticle.css');
		//lấy các category
		$this->view->arrCategories = $this->_categoryModel->getAllCategories();
		$this->view->arrManufacturers = $this->_manModel->getAllManufacturers();
		//kiểm tra có dữ liệu post lên không
		$request = $this->getRequest();
		//lấy id product
		$id  = $request->getParam('id');
		if(is_numeric($id)){
			$product = $this->_productModel->getProductByID($id);
			$this->view->product = $product;
			if($product){
				if($this->getRequest()->isPost()){
					//lấy dữ liệu trên form
					$name= $request->getParam('name');
					$model= $request->getParam('model');
					$price= $request->getParam('price');
					$show_on_home = 0;
					if($request->getParam('show_on_home')){
						$show_on_home = 1;
					}
					$image = $request->getParam('image');
					$image = join('@', $image);
					$specification= $request->getParam('specification');
					$ink= $request->getParam('ink');
					$slug = $request->getParam('slug');
					$components= $request->getParam('components');
					$description= $request->getParam('description');
					$short_description= $request->getParam('short_description');
					$page_title= $request->getParam('page_title');
					$page_description= $request->getParam('page_description');
					$category_id = $request->getParam('category_id');
					$feature_image = $request->getParam('feature_image');
					$created_date = date('Y-m-d', time());
					$man_id = $request->getParam('man_id');
					$tag = $request->getParam('tag');
					$filter = $request->getParam('filter');
					$rs = $this->_productModel->updateProduct($id, $name, $model, $price,$show_on_home, $image, $specification, $ink, $components, $description, $short_description, $page_title, $page_description, $created_date, $man_id, $slug, $category_id, $tag, $filter);
					if($rs){
						$session->message = '<span class="success">Sửa sản phẩm mới thành công</span>';
						$this->_redirector->gotoRoute(array(), 'admin_product_list');
					}else{
						$this->view->message = '<span class="error">Sửa sản phẩm mới không thành công</span>';
					}
				}
			}else{
				$session->message = '<span class="error">Không tồn tại sản phẩm này</span>';
			}
		}else{
			$session->message = '<span class="error">Không tồn tại sản phẩm này</span>';
		}
		
	}
	public function deleteAction(){
		$session = new Zend_Session_Namespace();
		if($this->getRequest()->isPost()){
			$id = $this->getRequest()->getParam('ids');
		}else{
			$id = $this->getRequest()->getParam('id');
		}
		if($this->_productModel->deleteProduct($id) > 0){
			$session->message = "<span class='success'>Xóa sản phẩm thành công</span>";
		}else{
			$session->message = "<span class='success'>Xóa sản phẩm không thành công</span>";
		}
		$this->_redirector->gotoRoute(array(), "admin_product_list");
	}
	
} 
?>